package com.ninghushangbao.ninghu.controller;

import com.ninghushangbao.ninghu.controller.vo.ResultEntity;
import com.ninghushangbao.ninghu.controller.vo.VerifyCode;
import com.ninghushangbao.ninghu.entity.News;
import com.ninghushangbao.ninghu.service.CarouselService;
import com.ninghushangbao.ninghu.service.NewsService;
import com.ninghushangbao.ninghu.service.ProductService;
import com.ninghushangbao.ninghu.utils.BaseResult.ResultUtil;
import com.ninghushangbao.ninghu.utils.VerifyUtils.IVerifyCodeGen;
import com.ninghushangbao.ninghu.utils.VerifyUtils.SimpleCharVerifyCodeGenImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/homepage")
public class homePageController {

    @Autowired
    CarouselService carouselService;

    @Autowired
    ProductService productService;

    @Autowired
    NewsService newsService;

    @GetMapping("captcha/get")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response)
    {
        IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
        try {
            //设置长宽
            VerifyCode verifyCode = iVerifyCodeGen.generate(80, 40);
            String code = verifyCode.getCode();
            //LOGGER.info(code);
            //将VerifyCode绑定session
            for (Cookie c : request.getCookies() != null ? request.getCookies() : new Cookie[0]) {
                System.out.println(c.getName() + "=" + c.getValue());
            }
            request.getSession().setAttribute("VerifyCode", code);
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException e) {
            //LOGGER.info("", e);
        }
    }

    @GetMapping("/company_content")
    public ResultEntity getCompanyContent(){
        return ResultUtil.success();
    }

    @GetMapping("/carousel_get")
    public ResultEntity getCarousel(){
        return ResultUtil.success(carouselService.selectCarousel());
    }

    @GetMapping("/product_get")
    public ResultEntity getProduct(){
        return ResultUtil.success(productService.selectProductHome());
    }

    @GetMapping("/news_company_get")
    public ResultEntity getNewsCompany(){
        return ResultUtil.success(newsService.selectNewsCompany());
    }

    @GetMapping("/news_group_get")
    public ResultEntity getNewsGroup(){
        return ResultUtil.success(newsService.selectNewsGroup());
    }

    @GetMapping("/news_topic_get")
    public ResultEntity getNewsTopic(){
        return ResultUtil.success(newsService.selectNewsTopic());
    }

    @GetMapping("/news_get_byId")
    public ResultEntity getNewsById(@RequestParam("newsId") int newsId){
        News news = newsService.selectNewsById(newsId);
        news.setNewsType(null);
        news.setUpdateTime(null);
        news.setLaunchTime(null);
        news.setStatus(null);
        System.out.println(news);
        return ResultUtil.success(news);
    }
}
